package com.woc.boot.admin.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woc.boot.admin.constant.ErrorCode;
import com.woc.boot.admin.entity.Response;
import com.woc.boot.srv.model.WocDomain;
import com.woc.boot.srv.service.WocDomainService;

/**
 * 域名管理控制器
 * 
 * @author     woc
 * @date       2024-01-26 
 * @version    0.0.1
 */
@RestController
@RequestMapping(value = "/domain")
public class DomainController {

    @Autowired
    private WocDomainService wocDomainService;

    @RequestMapping(value = {"", "/index"}, method = RequestMethod.GET)
    public Object index(@RequestParam(required = false, defaultValue = "0") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("id", id);
        mav.setViewName("domain/index");
        return mav;
    }

    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public Object list(@RequestParam(required = false, defaultValue = "0") Integer id,
            @RequestParam(required = false, defaultValue = "0") Integer offset,
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
        QueryWrapper<WocDomain> wrapper = new QueryWrapper<>();
        if (id > 0) {
        	wrapper.eq("id", id);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("total", wocDomainService.count(wrapper));
        wrapper.orderByDesc("id");
        wrapper.last(" limit " + offset + "," + limit);
        result.put("rows", wocDomainService.list(wrapper));
        return result;
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public Response<Object> info(Integer id) {
    	Response<Object> resp = new Response<>();
        if (id == null || id <= 0) {
            return resp.withError(ErrorCode.ParamsError);
        }
        WocDomain domain = wocDomainService.getById(id);
        if (domain == null) {
            return resp.withError("域名查询失败");
        }
        return resp.withOk(domain);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Response<Object> save(@Validated @RequestBody WocDomain domain) {
        Response<Object> resp = new Response<>();
        if (domain == null || StringUtils.isBlank(domain.getName())) {
            return resp.withError(ErrorCode.ParamsError);
        }
        QueryWrapper<WocDomain> wrapper = new QueryWrapper<>();
        wrapper.eq("name", domain.getName());
        WocDomain exist = wocDomainService.getOne(wrapper);
        if (domain.getId() == null) {
            if (exist != null) {
                return resp.withError("域名不能重复");
            }
            if (!wocDomainService.save(domain)) {
                return resp.withError("域名新增失败");
            }
        } else {
            if (exist != null && !exist.getId().equals(domain.getId())) {
                return resp.withError("域名不能重复");
            }
            if (!wocDomainService.updateById(domain)) {
                return resp.withError("域名更新失败");
            }
        }
        return resp.withOk();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Response<Object> delete(Integer id) {
        Response<Object> resp = new Response<>();
        if (id == null || id <= 0) {
            return resp.withError(ErrorCode.ParamsError);
        }
        if (!wocDomainService.removeById(id)) {
            return resp.withError("域名删除失败");
        }
        return resp.withOk();
    }
}
